<?php
  require('includes/application_top.php');
  
  function delete_directory($dirname) {
    if (is_dir($dirname))
      $dir_handle = opendir($dirname);
    if (!$dir_handle)
      return false;
    while($file = readdir($dir_handle)) {
      if ($file != "." && $file != "..") {
         if (!is_dir($dirname."/".$file))
            unlink($dirname."/".$file);
        else
           delete_directory($dirname.'/'.$file);    
     }
    }
    closedir($dir_handle);
    rmdir($dirname);
    return true;
}  

  
  $action = (isset($HTTP_GET_VARS['action']) ? $HTTP_GET_VARS['action'] : '');
  if (tep_not_null($action)) {
    switch ($action) {        
        case 'published':
            tep_db_query("update ushop_widgets set published = '1' where id = '" . (int)$HTTP_GET_VARS['wID'] . "'");
            tep_redirect(tep_href_link('widgets.php'));
        break;
        
        case 'unpublished':
            tep_db_query("update ushop_widgets set published = '0' where id = '" . (int)$HTTP_GET_VARS['wID'] . "'");
            tep_redirect(tep_href_link('widgets.php'));
        break;
        
        case 'addPos':
            tep_db_query("update ushop_widgets set addPos = '1' where id = '" . (int)$HTTP_GET_VARS['wID'] . "'");
            tep_redirect(tep_href_link('widgets.php'));
        break;
        
        case 'unaddPos':
            tep_db_query("update ushop_widgets set addPos = '0' where id = '" . (int)$HTTP_GET_VARS['wID'] . "'");
            tep_redirect(tep_href_link('widgets.php'));
        break;
        
        case 'delete':
            $id = tep_db_prepare_input($HTTP_GET_VARS['wID']);
            
            $folders_query = tep_db_query("select folders from ushop_widgets where id = '" . (int)$id . "'");
            $folders = tep_db_fetch_array($folders_query);
            //echo $folders['folders'].'<br />';
            /*$splitter = "/";
            $wid_name = explode("/", substr($folders['folders'], 0, strrpos($folders['folders'], $splitter)));
            echo $wid_name[2];*/
            //delete_directory($wid_name[2]);
            delete_directory($folders['folders']);
            
            tep_db_query("delete from ushop_widgets where id = '" . (int)$id . "'");
    
            tep_redirect(tep_href_link('widgets.php'));
        break;    
    }
  } 
?>
<!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN">
<html <?php echo HTML_PARAMS; ?>>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=<?php echo CHARSET; ?>">
<title><?php echo TITLE; ?></title>
<link rel="stylesheet" type="text/css" href="includes/stylesheet.css">
<script language="javascript" src="includes/general.js"></script>

<!-- Begin widgets -->
<link type="text/css" href="http://jqueryui.com/themes/base/jquery.ui.all.css" rel="stylesheet" />
<link type="text/css" href="ext/widgets/css/widgets.css" rel="stylesheet" />

<script type="text/javascript" src="ext/widgets/js/jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="ext/widgets/js/ui/jquery.ui.core.js"></script>
<script type="text/javascript" src="ext/widgets/js/ui/jquery.ui.widget.js"></script>
<script type="text/javascript" src="ext/widgets/js/ui/jquery.ui.mouse.js"></script>
<script type="text/javascript" src="ext/widgets/js/ui/jquery.ui.sortable.js"></script>

<script type="text/javascript">
// When the document is ready set up our sortable with it's inherant function(s)
jQuery(document).ready(function() {
    
    jQuery("#list").sortable({
        connectWith: '.handle',
        update : function () {
            var order = jQuery('#list').sortable('serialize');
            var area = "";
            jQuery("#info").load("widgets-process-sortable.php?"+order+"&area="+area);
        }
    });
    
    jQuery("#list-top").sortable({
        connectWith: '.handle',
        update : function () {
            var order = jQuery('#list-top').sortable('serialize');
            var area = "top";
            jQuery("#info").load("widgets-process-sortable.php?"+order+"&area="+area);
        }
    });
    
    jQuery("#list-search").sortable({
        connectWith: '.handle',
        update : function () {
            var order = jQuery('#list-search').sortable('serialize');
            var area = "search";
            jQuery("#info").load("widgets-process-sortable.php?"+order+"&area="+area);
        }
    });
    
    jQuery("#list-left").sortable({
        connectWith: '.handle',
        update : function () {
            var order = jQuery('#list-left').sortable('serialize');
            var area = "left";
            jQuery("#info").load("widgets-process-sortable.php?"+order+"&area="+area);
        }
    });
    
    jQuery("#list-breadcrumbs").sortable({
        connectWith: '.handle',
        update : function () {
            var order = jQuery('#list-breadcrumbs').sortable('serialize');
            var area = "breadcrumbs";
            jQuery("#info").load("widgets-process-sortable.php?"+order+"&area="+area);
        }
    });
    
    jQuery("#list-cartbox").sortable({
        connectWith: '.handle',
        update : function () {
            var order = jQuery('#list-cartbox').sortable('serialize');
            var area = "cartbox";
            jQuery("#info").load("widgets-process-sortable.php?"+order+"&area="+area);
        }
    });
    
    jQuery("#list-welcome").sortable({
        connectWith: '.handle',
        update : function () {
            var order = jQuery('#list-welcome').sortable('serialize');
            var area = "welcome";
            jQuery("#info").load("widgets-process-sortable.php?"+order+"&area="+area);
        }
    });
    
    jQuery("#list-google").sortable({
        connectWith: '.handle',
        update : function () {
            var order = jQuery('#list-google').sortable('serialize');
            var area = "google";
            jQuery("#info").load("widgets-process-sortable.php?"+order+"&area="+area);
        }
    });
    
    jQuery(".column").sortable({
		connectWith: '.column'
	});
    //ui-widget-content 
	jQuery(".handle").addClass("ui-widget ui-helper-clearfix ui-corner-all")
		.find(".portlet-header")
			.addClass("ui-widget-header ui-corner-all")
			.prepend('<span class="ui-icon ui-icon-minusthick"></span>')
			.end()
		.find(".portlet-content");

	jQuery(".portlet-header .ui-icon").click(function() {
		jQuery(this).toggleClass("ui-icon-minusthick").toggleClass("ui-icon-plusthick");
		jQuery(this).parents(".handle:first").find(".portlet-content").toggle();
	});

	jQuery(".column").disableSelection();
    
});
</script>
<!-- End widgets -->
</head>
<body marginwidth="0" marginheight="0" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0" bgcolor="#FFFFFF" onload="SetFocus();">
<div id="spiffycalendar" class="text"></div>
<!-- header //-->
<?php require(DIR_WS_INCLUDES . 'header.php'); ?>
<!-- header_eof //-->

<!-- body //-->
<table border="0" width="100%" cellspacing="2" cellpadding="2">
  <tr>
    <td width="<?php echo BOX_WIDTH; ?>" valign="top">
    <table border="0" width="<?php echo BOX_WIDTH; ?>" cellspacing="1" cellpadding="1" class="columnLeft">
<!-- left_navigation //-->
<?php require(DIR_WS_INCLUDES . 'column_left.php'); ?>
<!-- left_navigation_eof //-->
    </table></td>
<!-- body_text //-->
    <td valign="top">

Danh sách các Widgets:<br /><br />
<form name="widgets_re" action="widgets_remove.php" method="post">
<table border="0" cellpadding="0" cellspacing="0" width="100%" style="font-size: 12px;">
    <tr bgcolor="#B3BAC5" style="font-weight: bold;">
        <td>Tên</td>
        <td>Mô tả</td>
        <td>Vùng hiển thị</td>
        <td>Sắp xếp</td>
        <td>Kích hoạt</td>
        <td></td>
    </tr>
    <tr>
<?php 
    $widgets_query_raw = "select * from ushop_widgets order by name";
    $widgets_split = new splitPageResults($HTTP_GET_VARS['page'], MAX_DISPLAY_SEARCH_RESULTS, $widgets_query_raw, $widgets_query_numrows);
    $widgets_query = tep_db_query($widgets_query_raw);
    while ($widgets = tep_db_fetch_array($widgets_query)) {
        echo '<td>'.$widgets['name'];
        if($widgets['area']=='google'){
            echo ' - <b>Thêm vị trí:</b> ';
            if($widgets['addPos'] == 0 ){
                echo '<a href="' . tep_href_link('widgets.php', 'wID=' . $widgets['id'] . '&action=addPos') . '">Không</a>';
            }else{
                echo '<a href="' . tep_href_link('widgets.php', 'wID=' . $widgets['id'] . '&action=unaddPos') . '">Có</a>';    
            }
        }
        echo '</td>';
        echo '<td>'.$widgets['description'].'</td>';
        if($widgets['area'] == ""){
            echo '<td>Chưa được hiển thị lên website.</td>';
        }else{
            echo '<td>Hiển thị tại <b>'.$widgets['area'].'</b></td>';    
        }
        echo '<td>'.$widgets['ordering'].'</td>';
        if($widgets['published'] == 0){
            echo '<td><a href="' . tep_href_link('widgets.php', 'wID=' . $widgets['id'] . '&action=published') . '">Chưa kích hoạt</a></td>';
        }else{
            echo '<td><a href="' . tep_href_link('widgets.php', 'wID=' . $widgets['id'] . '&action=unpublished') . '">Đã kích hoạt</a></td>';    
        }
        echo '<td><a href="' . tep_href_link('widgets.php', 'wID=' . $widgets['id'] . '&action=delete') . '">Xóa</a></td>';
        
        echo '</tr>';   
    }
?>
        <tr>
            <td class="smallText" valign="top" align="right" colspan="6">
                <?php echo $widgets_split->display_count($widgets_query_numrows, MAX_DISPLAY_SEARCH_RESULTS, $HTTP_GET_VARS['page'], TEXT_DISPLAY_NUMBER_OF_ORDERS); ?>
                <?php echo $widgets_split->display_links($widgets_query_numrows, MAX_DISPLAY_SEARCH_RESULTS, MAX_DISPLAY_PAGE_LINKS, $HTTP_GET_VARS['page']); ?>
            </td>
        </tr>
</form>
<tr><td style="padding-bottom: 20px;"></td></tr>    
</table>
    
<!--<pre><div id="info">Waiting for update to database ushop_widgets</div></pre>-->

<div class="widgets-list">    
    <div>
        <font class="title-area">Widgets Area</font>
        <br />
        <i>Widgets area description</i>
    </div>
    <div id="list" class="column">
        <?php
            $widgets_query = tep_db_query("select * from ushop_widgets where area = '' order by ordering");
            while ($widgets = tep_db_fetch_array($widgets_query)) {
            ?>
                <div id="listItem_<?php echo $widgets['id']; ?>" class="handle">
            		<div class="portlet-header"><?php echo $widgets['name']; ?></div>
            		<div class="portlet-content"><?php echo $widgets['description']; ?></div>
            	</div>                    
            <?php
            }
        ?>  
    </div>
</div>

<div class="widgets-site-list">
    <div>
        <font class="title-area">Top Area</font>
        <br />
        <i>Top area description</i>
    </div>
    <div id="list-top" class="column">       
        <?php
            $widgets_query = tep_db_query("select * from ushop_widgets where area = 'top' order by ordering");
            while ($widgets = tep_db_fetch_array($widgets_query)) {
            ?>
                <div id="listItem_<?php echo $widgets['id']; ?>" class="handle">
            		<div class="portlet-header"><?php echo $widgets['name']; ?></div>
            		<div class="portlet-content"><?php echo $widgets['description']; ?></div>
            	</div>
            <?php
            }
        ?>
    </div>
       
    <div>
        <font class="title-area">Search Area</font>
        <br />
        <i>Search area description</i>
    </div>
    <div id="list-search" class="column">       
        <?php
            $widgets_query = tep_db_query("select * from ushop_widgets where area = 'search' order by ordering");
            while ($widgets = tep_db_fetch_array($widgets_query)) {
            ?>
                <div id="listItem_<?php echo $widgets['id']; ?>" class="handle">
            		<div class="portlet-header"><?php echo $widgets['name']; ?></div>
            		<div class="portlet-content"><?php echo $widgets['description']; ?></div>
            	</div>
            <?php
            }
        ?>
    </div>
    
    <div>
        <font class="title-area">Left Area</font>
        <br />
        <i>Left area description</i>
    </div>
    <div id="list-left" class="column">       
        <?php
            $widgets_query = tep_db_query("select * from ushop_widgets where area = 'left' order by ordering");
            while ($widgets = tep_db_fetch_array($widgets_query)) {
            ?>
                <div id="listItem_<?php echo $widgets['id']; ?>" class="handle">
            		<div class="portlet-header"><?php echo $widgets['name']; ?></div>
            		<div class="portlet-content"><?php echo $widgets['description']; ?></div>
            	</div>
            <?php
            }
        ?>
    </div>
</div>

<div class="widgets-site-list">    
    <div>
        <font class="title-area">Breadcrumbs Area</font>
        <br />
        <i>Breadcrumbs area description</i>
    </div>
    <div id="list-breadcrumbs" class="column">       
        <?php
            $widgets_query = tep_db_query("select * from ushop_widgets where area = 'breadcrumbs' order by ordering");
            while ($widgets = tep_db_fetch_array($widgets_query)) {
            ?>
                <div id="listItem_<?php echo $widgets['id']; ?>" class="handle">
            		<div class="portlet-header"><?php echo $widgets['name']; ?></div>
            		<div class="portlet-content"><?php echo $widgets['description']; ?></div>
            	</div>
            <?php
            }
        ?>
    </div>
    
    <div>
        <font class="title-area">Cart box Area</font>
        <br />
        <i>Cart box area description</i>
    </div>
    <div id="list-cartbox" class="column">       
        <?php
            $widgets_query = tep_db_query("select * from ushop_widgets where area = 'cartbox' order by ordering");
            while ($widgets = tep_db_fetch_array($widgets_query)) {
            ?>
                <div id="listItem_<?php echo $widgets['id']; ?>" class="handle">
            		<div class="portlet-header"><?php echo $widgets['name']; ?></div>
            		<div class="portlet-content"><?php echo $widgets['description']; ?></div>
            	</div>
            <?php
            }
        ?>
    </div>
    
    <div>
        <font class="title-area">Welcome Area</font>
        <br />
        <i>Welcome area description</i>
    </div>
    <div id="list-welcome" class="column">       
        <?php
            $widgets_query = tep_db_query("select * from ushop_widgets where area = 'welcome' order by ordering");
            while ($widgets = tep_db_fetch_array($widgets_query)) {
            ?>
                <div id="listItem_<?php echo $widgets['id']; ?>" class="handle">
            		<div class="portlet-header"><?php echo $widgets['name']; ?></div>
            		<div class="portlet-content"><?php echo $widgets['description']; ?></div>
            	</div>
            <?php
            }
        ?>
    </div>
    
    <div>
        <font class="title-area">Google Area</font>
        <br />
        <i>Google area description</i>
    </div>
    <div id="list-google" class="column">       
        <?php
            $widgets_query = tep_db_query("select * from ushop_widgets where area = 'google' order by ordering");
            while ($widgets = tep_db_fetch_array($widgets_query)) {
            ?>
                <div id="listItem_<?php echo $widgets['id']; ?>" class="handle">
            		<div class="portlet-header"><?php echo $widgets['name']; ?></div>
            		<div class="portlet-content"><?php echo $widgets['description']; ?></div>
            	</div>
            <?php
            }
        ?>
    </div>
</div>

    </td>
<!-- body_text_eof //-->
  </tr>
</table>
<!-- body_eof //-->

<!-- footer //-->
<?php require(DIR_WS_INCLUDES . 'footer.php'); ?>
<!-- footer_eof //-->
<br>
</body>
</html>
<?php require(DIR_WS_INCLUDES . 'application_bottom.php'); ?>
